package org.link.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.link.bean.Board;
import org.link.service.BoardService;

import com.alibaba.fastjson.JSON;

/**
 * 
 * project_name:forumweb
 * package_name:org.link.web
 * file_name:BoardServlet.java
 * @author Link
 * create_date:2015年10月23日 上午11:46:58
 * modified_date:
 * description: 板块信息处理Servlet
 *
 */
public class BoardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BoardService boardService = new BoardService();
	
	/**
	 * 总接收Servlet
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String str = request.getParameter("op");
		if(str.equals("checkFatherPlate")){
			checkFatherPlateServlet(request, response);
		}else if(str.equals("addFatherPlate")){
			addFatherPlate(request, response);
		}else if(str.equals("addChildPlate")){
			addChildPlate(request, response);
		}else if(str.equals("queryChildBoard")){
			queryChildBoard(request, response);
		}else if(str.equals("loadFatherBoard")){
			loadFatherBoard(request, response);
		}
	}
    
	/**
	 * 
	 * description:切换到我要发帖的时候加载父板块
	 * 2015年10月27日 下午2:55:30
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 *
	 */
	private void loadFatherBoard(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		ArrayList<Board> allFatherBoard = boardService.queryFatherBoard();
		String board = JSON.toJSONString(allFatherBoard);
		PrintWriter out = response.getWriter();
		out.print(board);
	}

	/**
	 * 
	 * description: 根据接收到的父板块返回所有子版块集合
	 * 2015年10月27日 上午11:35:04
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 *
	 */
	private void queryChildBoard(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String fatherBoradName = request.getParameter("fatherBoard");
		ArrayList<Board> childBoardList = boardService.queryChildBoard(fatherBoradName);
		String boardJson = JSON.toJSONString(childBoardList);//转换成JSON字符串对象	
		PrintWriter out = response.getWriter();
		out.print(boardJson);
	}

	/**
	 * 
	 * description: 添加子版块
	 * creatTime:2015年10月23日下午11:47:58
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void addChildPlate(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String fatherBoardName = request.getParameter("fatherPlateName");
		String childBoardName = request.getParameter("childPlateName");
		String childBoardDesc = request.getParameter("childPlateDesc");
		HttpSession session = request.getSession();
		if(boardService.addchildBoard(fatherBoardName,childBoardName,childBoardDesc)){
			session.setAttribute("allBoard", boardService.QueryAllBoard());
			response.sendRedirect("pages/home.jsp");
		}
	}

	/**
	 * 
	 * description: 添加父板块
	 * 2015年10月23日 下午5:24:23
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 *
	 */
	private void addFatherPlate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String boardName = request.getParameter("fatherPlateName");
		String boardDesc = request.getParameter("fatherPlateDesc");
		HttpSession session = request.getSession();
		ArrayList<Board> boards = boardService.addAndQueryFatherBoard(boardName, boardDesc);
		if(boards!=null){
			session.setAttribute("allBoard", boards);
			session.setAttribute("allFatherBoard", boardService.queryAllFatherBoard());
			response.sendRedirect("pages/home.jsp");
		}else{
			session.setAttribute("addBoard", "添加板块信息失败!");
			session.setAttribute("allFatherBoard", boardService.queryAllFatherBoard());
			response.sendRedirect("pages/home.jsp");
		}
	}

	/**
	 * 
	 * description: 查看父板块是否存在
	 * 2015年10月23日 上午11:54:16
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 *
	 */
	private void checkFatherPlateServlet(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	PrintWriter out = response.getWriter();
    	String fatherBoardName = request.getParameter("fatherPlate"); 
    	if(boardService.checkBoardExist(fatherBoardName)){
    		out.print("exist");
    	}else{
    		out.print("inexistence");
    	}
    }
}









